<?php include_once ('function/check_login_function.php'); onlyValid(); onlyPatient();?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Health Tracker</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Health Tracker">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <link rel="shortcut icon" href="ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
  </head>

  <body>
        <?php include_once('nav.php'); ?>
    <div class="container">
    <div class="row">
    <div class="span8">
      <h1>Food Diary</h1>
	  
					<?php
	  					if (isset($_SESSION['message'])) {
						foreach ($_SESSION['message'] as $message) {
							echo '<div class="alert">'.constant($message) . '</div>';
						}
						unset($_SESSION['message']);
					} 
					?>
				<?php if ($user->getRole() === 'patient') {?>
					
					<?php include_once('food_form.php'); ?>
					
					<?php $food_list = $user->getFoodList(); ?>
					<?php if ($food_list !== null) {?>
					<table class="table">
					<tr>
					<th>Name</th>
					<th>Calories (kCal)</th>
					<th>Notes</th>
					<th>Date</th>
					<th></th>
					</tr>
					<?php 
						
							foreach ($food_list as $food) {
								echo '<tr>';
								echo '<td>' . $food['food_name'] . '</td>';
								echo '<td>' . $food['food_calories'] . '</td>';
								echo '<td>' . $food['food_note'] . '</td>';
								echo '<td>' . $food['created_at'] . '</td>';
								echo '<td>';
									echo '<form action="function/form_function.php" method="POST">';
									echo '<input type="hidden" id="foodID" name="foodID" value="' . $food['food_id'] . '"/>';
									echo '<input type="hidden" id="from" name="from" value="food"/>';
									echo '<input type="submit" name="removeFood" value="Remove Food" class="btn btn-primary"/> ';
									echo' </form>';
								echo '</td>';
								echo '</tr>';
							}
						}
					?>
					</table>
				<?php } ?>
				
				<?php 
					$calories_list = $user->getFoodCaloriesPerDay();
					if ($calories_list !== null) {
						$first_time = strtotime($calories_list[0]['food_date']);
						$last_time = strtotime($calories_list[count($calories_list)-1]['food_date']);
						for ($i = $first_time; $i <= $last_time; $i += 86400) {
							$food_calories = null;
							foreach ($calories_list as $calories) {
								$food_time = strtotime($calories['food_date']);
								if ($food_time == $i) {
									$food_calories = $calories['food_calories'];
									break;
								}
							}
							$data[] = array(date("ymd", $i), $food_calories);
						}

					
						$graph = new PHPlot(GRAPH_WIDTH_LARGE, GRAPH_HEIGHT_LARGE); 
						
						 //Specify this random file to be the output file of the image
						$temp_png = GRAPH_DIR . "graph_calories_" . $user->getUserID() . ".png";
						$graph->SetOutputfile($temp_png);
						$graph->SetFileFormat(GRAPH_FILEFORMAT); // is default anyway 
						
						//Don't draw the image yet
						$graph->SetPrintImage(0);
						//Don't output the file headers - we're inline!
						$graph->SetIsInline("1"); 
						$graph->SetDataType(GRAPH_DATATYPE);
						$graph->SetDataValues($data);

						//$graph->SetDefaultTTFont("graph/arial.ttf");
						//Specify plotting area details
						$graph->SetPlotType(GRAPH_PLOTTYPE);
						$graph->SetYTickIncrement(GRAPH_YTICK_INCREMENT);
						$graph->SetXTickIncrement(GRAPH_XTICK_INCREMENT);
						
						$graph->SetTTFPath(GRAPH_TTFPATH);			
						$graph->SetDefaultTTFont(GRAPH_TTFFONT);
						
						$graph->SetFontTTF('generic', GRAPH_TTFFONT, GRAPH_TTFFONT_GENERIC);
						$graph->SetFontTTF('legend', GRAPH_TTFFONT, GRAPH_TTFFONT_LEGEND);
						$graph->SetFontTTF('title', GRAPH_TTFFONT, GRAPH_TTFFONT_TITLE);						
						$graph->SetFontTTF('x_label', GRAPH_TTFFONT, GRAPH_TTFFONT_XLABEL);
						$graph->SetFontTTF('y_label', GRAPH_TTFFONT, GRAPH_TTFFONT_YLABEL);
						$graph->SetFontTTF('x_title', GRAPH_TTFFONT, GRAPH_TTFFONT_XTITLE);
						$graph->SetFontTTF('y_title', GRAPH_TTFFONT, GRAPH_TTFFONT_YTITLE);
						
						$graph->SetPlotBgColor(GRAPH_PLOT_BGCOLOR);
						$graph->SetBackgroundColor(GRAPH_BGCOLOR); 
						$graph->SetPlotBorderType(GRAPH_PLOT_BORDERTYPE);
						 
						$graph->SetTitle('Time over calories');
						
						//Define the X axis
						$graph->SetXLabel( "Days");
						$graph->SetXDataLabelPos('plotdown');
						  
						//Define the Y axis
						$graph->SetYLabel( "Calories");
						$graph->SetDataColors(unserialize(GRAPH_DATA_COLORS)); 
						$graph->DrawGraph();
						$graph->PrintImage();
						
						$graph_width=GRAPH_WIDTH;
						$graph_height=GRAPH_HEIGHT;
						echo "<p align=\"center\"><img src=\"{$temp_png}\" width=\"{$graph_width}\" height=\"{$graph_height}\"> </p>"; 
					} 
				?>
    </div>
    </div>
    </div> <!-- /container -->
  </body>
</html>